<template>
<div class="ivu-card ivu-card-bordered ivu-card-dis-hover">
  <div class="ivu-card-head"><p>{{ $route.meta.title }}</p></div>

  <div class="ivu-card-body">
    <Input placeholder="请输入用户名" style="width: 20%" v-model="searchAccount">
      <Button slot="append" type="primary" icon="search" @click="clickSearch">搜索</Button>
    </Input>
  </div>

  <div class="ivu-card-body"><Table :columns="list.columns" :data="list.items" :loading="list.loading" /></div>

  <div class="ivu-card-body"><Page :current="pager.current" :total="pager.totalItems" :page-size="pager.limit" show-total @on-change="onChangePage"></Page></div>
</div>
</template>
<script>
import { Table, Page, Message, Input, Button } from 'iview';
import ajaxiView from '@/utils/ajaxiView';
import apiConfig from '@/utils/apiConfig';
import config from '@/config';
import auth from '@/utils/auth';

export default {
  components: {
    [Table.name]: Table,
    [Page.name]: Page,
    [Input.name]: Input,
    [Button.name]: Button
  },
  data() {
    return {
      searchAccount: '',
      apiConfig: {},
      config,
      token: auth.getToken(),
      list: {
        columns: [
          {title: 'ID', align: 'center', render: (h, params) => { return h('span', params.row.account_wallet_transfer.id); }},
          {title: '类型', align: 'center', render: (h, params) => {
            switch(params.row.account_wallet_transfer.field) {
              case 4:
              case '4':
                return h('span', '交易票');
              case 5:
              case '5':
                return h('span', '开户票');
            }
          }},
          {title: '数量', align: 'center', render: (h, params) => { return h('span', params.row.account_wallet_transfer.amount); }},
          {title: '来自用户', key: 'from_account_account', align: 'center'},
          {title: '转到用户', key: 'to_account_account', align: 'center'},
          {title: '创建时间', align: 'center', render: (h, params) => { return h('span', new Date(parseInt(params.row.account_wallet_transfer.create_time) * 1000).toLocaleString()); }}
        ],
        loading: false,
        items: []
      },
      pager: {
        current: 1,
        limit: 10,
        totalItems: 0
      }
    };
  },
  methods: {
    _initialize() {
      var _this = this;
      apiConfig.getItem().then(function(api_config) {
        _this.apiConfig = api_config;
        _this.getData();
      });
    },
    getData() {
      let _this = this;
      _this.list.loading = true;
      ajaxiView.get('/backstage/user/wallet/transfer/log?token='+_this.token+'&page='+_this.pager.current+'&account='+_this.searchAccount).then(function(data) {
        if(data !== false && typeof(data.pager) != 'undefined') {
          _this.list.items = data.pager.items;
          _this.pager.limit = data.pager.limit;
          _this.pager.totalItems = data.pager.total_items;

          _this.list.loading = false;
        }
      });
    },
    onChangePage(current) {
      this.pager.current = current;
      this.getData();
    },
    clickSearch() {
      if (this.searchAccount === '') {
        Message.warning('请输入用户名');
      } else {
        this.onChangePage(1);
      }
    }
  },
  mounted() {
    this._initialize();
  }
}
</script>
<style>
.ivu-table-cell .ivu-btn.ivu-btn-small {
  margin-right: 5px;
}
</style>